package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.INodeRoleService;
import com.mdd.admin.validate.NodeRoleCreateValidate;
import com.mdd.admin.validate.NodeRoleUpdateValidate;
import com.mdd.admin.validate.NodeRoleSearchValidate;
import com.mdd.admin.vo.NodeRoleListedVo;
import com.mdd.admin.vo.NodeRoleDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.NodeRole;
import com.mdd.common.mapper.NodeRoleMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 节点用户、角色、岗位关联实现类
 * @author LikeAdmin
 */
@Service
public class NodeRoleServiceImpl implements INodeRoleService {
        
    @Resource
    NodeRoleMapper nodeRoleMapper;

    /**
     * 节点用户、角色、岗位关联列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<NodeRoleListedVo>
     */
    @Override
    public PageResult<NodeRoleListedVo> list(PageValidate pageValidate, NodeRoleSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<NodeRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        nodeRoleMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:nodeId@node_id:int",
            "=:roleId@role_id:int",
            "=:adminUserId@admin_user_id:int",
            "=:postId@post_id:int",
            "=:userId@user_id:int",
            "like:userName@user_name:str",
            "=:updateUserId@update_user_id:int",
            "like:updateUserName@update_user_name:str",
        });

        IPage<NodeRole> iPage = nodeRoleMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<NodeRoleListedVo> list = new LinkedList<>();
        for(NodeRole item : iPage.getRecords()) {
            NodeRoleListedVo vo = new NodeRoleListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 节点用户、角色、岗位关联详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return NodeRole
     */
    @Override
    public NodeRoleDetailVo detail(Integer id) {
        NodeRole model = nodeRoleMapper.selectOne(
                new QueryWrapper<NodeRole>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        NodeRoleDetailVo vo = new NodeRoleDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 节点用户、角色、岗位关联新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(NodeRoleCreateValidate createValidate) {
        NodeRole model = new NodeRole();
        model.setNodeId(createValidate.getNodeId());
        model.setRoleId(createValidate.getRoleId());
        model.setAdminUserId(createValidate.getAdminUserId());
        model.setPostId(createValidate.getPostId());
        model.setUserId(createValidate.getUserId());
        model.setUserName(createValidate.getUserName());
        model.setUpdateUserId(createValidate.getUpdateUserId());
        model.setUpdateUserName(createValidate.getUpdateUserName());
        nodeRoleMapper.insert(model);
    }

    /**
     * 节点用户、角色、岗位关联编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(NodeRoleUpdateValidate updateValidate) {
        NodeRole model = nodeRoleMapper.selectOne(
                new QueryWrapper<NodeRole>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setNodeId(updateValidate.getNodeId());
        model.setRoleId(updateValidate.getRoleId());
        model.setAdminUserId(updateValidate.getAdminUserId());
        model.setPostId(updateValidate.getPostId());
        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        model.setUpdateUserId(updateValidate.getUpdateUserId());
        model.setUpdateUserName(updateValidate.getUpdateUserName());
        nodeRoleMapper.updateById(model);
    }

    /**
     * 节点用户、角色、岗位关联删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        NodeRole model = nodeRoleMapper.selectOne(
                new QueryWrapper<NodeRole>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        nodeRoleMapper.updateById(model);
    }

}
